from flask import Flask, request, jsonify
from flask_cors import CORS  # 导入Flask-CORS库
import random
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.14.0
from dashscope import Generation
messages = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
app = Flask(__name__)
CORS(app) 
@app.route('/taskindex/newsindex', methods=['POST'])
def get_response():
    # 获取用户输入的消息
    messages.append({'role': 'user', 'content':  request.get_json().get('message')})
    # 调用Dashscope SDK的call函数生成回复内容
    responses = Generation.call(model="qwen-turbo",
                                api_key="sk-3c0aee84e5b74dfcb5e36b4b1679a97e",  # 替换为你的API密钥
                               messages=messages,
                                # 设置输出方式为流式输
                                stream=True,
                                # 增量式流式输出
                                incremental_output=True,
                               # 将输出设置为"message"格式
                          result_format='message')
    
    
    full_content = []
    for response in responses:
        if response.status_code == HTTPStatus.OK:
         full_content.append(response)
         print(full_content)
            
    #         full_content += response.output.choices[0].message.content
    #     else:
    #         print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
    #             response.request_id, response.status_code,
    #             response.code, response.message
    #         ))
 
    # messages.append({'role': 'assistant', 'content': response['output']['choices'][0]['message']['content']}) 
    return full_content

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=5000)